#! /bin/bash
NOW_DIR=$(cd -P $(dirname $0);pwd)
DOCKER_DIR=$NOW_DIR
NGINX_DIR=$NOW_DIR/nginx/cert

#开启站点
WebKeyArr=$@

WebArr=(dev.au.callie.com dev.ca.callie.com dev.fr.callie.com dev.uk.callie.com dev.www.callie.com dev.www.callie.de dev.it.callie.com dev.nl.callie.com dev.www.callie.es dev1.www.callie.com dev1.www.callie.de);

#进入nginx目录执行
cd $NGINX_DIR

# 遍历数组并获取键和值
for index in "${!WebArr[@]}"; do
  key="$index"
  domain="${WebArr[$index]}"
  echo "键：$key，值：$domain"
  if [[ ! " ${WebKeyArr[*]} " =~ " $key " ]]; then
    echo "$domain 站点不改变证书"
    continue
  fi
  echo "$domain 站点准备改变证书"
  #docker站点目录
  siteDockerDir=${DOCKER_DIR}/$domain
  chmod -R 777 "${DOCKER_DIR}"

  echo "Create server key..."
  openssl genrsa -des3 -out $domain.key 4096
  echo "Create server certificate signing request..."
  SUBJECT="/C=US/ST=Mars/L=iTranswarp/O=iTranswarp/OU=iTranswarp/CN=$domain"
  openssl req -new -subj $SUBJECT -key $domain.key -out $domain.csr
  echo "Remove password..."
  mv $domain.key $domain.origin.key
  openssl rsa -in $domain.origin.key -out $domain.key

  echo "Sign SSL certificate..."

  openssl x509 -req -days 3650 -in $domain.csr -signkey $domain.key -out $domain.crt

	cp -ar $domain.crt $siteDockerDir/files/nginx1.21.6/conf.d/cert/dev.crt
	cp -ar $domain.key $siteDockerDir/files/nginx1.21.6/conf.d/cert/dev.key

  echo "开始重启站点"
  cd "${siteDockerDir}" && docker-compose down
  cd "${siteDockerDir}" && docker-compose up -d
  echo "启动 ${domain} 站点成功！！！"
done

